package com.mallsystem.orderservice.service;

import com.mallsystem.common.bean.Cart;
import com.mallsystem.common.bean.Goods;
import com.mallsystem.common.exception.AllException;
import com.mallsystem.common.service.CartService;
import com.mallsystem.common.service.GoodsService;
import com.mallsystem.orderservice.mapper.CartMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @DubboReference
    private GoodsService goodsService;
    @Override
    public boolean insertOrUpdate(Cart cart) {
        //查询购物车中是否存在该记录 1.存在 - 累加 2.不存在 - 添加
        Cart c = cartMapper.selectByUserIdAndGoodsId(cart.getUserId(), cart.getGoodsId());

        if(c == null){
            //添加
            cart.setCount(1);
            return cartMapper.insert(cart) == 1;
        }else {
            //修改 - 累加
            c.setCount(c.getCount() + 1);
            return cartMapper.update(c) == 1;
        }
    }

    @Override
    public boolean delete(Integer id, Integer userId) throws AllException {
        //判断购物车是否存在
        Cart cart = cartMapper.selectById(id);
        if (cart == null) {
            throw new AllException("该购物车不存在,无法删除");
        }
        //判断被删除的购物车是否属于当前用户
        if (!cart.getUserId().equals(userId)) {
            throw new AllException("该购物车不属于当前用户,无法删除");
        }
        //删除
        return cartMapper.delete(id) == 1;
    }

    @Override
    public boolean update(Cart cart,Integer userId) throws AllException {
        //判断该购物车是否存在
        Cart c = cartMapper.selectById(cart.getId());
        if(c == null){
            throw new AllException("该购物车记录不存在，无法修改");
        }
        //判断被修改的购物车是否属于当前用户
        if (!c.getUserId().equals(userId)) {
            throw new AllException("该购物车不属于当前用户,无法修改");
        }
        //调用goods_service查询购物车对应的商品
        //判断库存是否充足
        Goods goods = goodsService.selectById(c.getGoodsId());
        if(cart.getCount() != null && cart.getCount() > goods.getCount()){
            //当购物车商品的数量超过库存，就设置购物车商品数量为库存数量
            cart.setCount(goods.getCount());
        }
        return cartMapper.update(cart) == 1;
    }

    @Override
    public List<Cart> search(Cart condition) {
        List<Cart> cartsList = cartMapper.selectByCondition(condition);
        //调用goods_service查询购物车对应的商品
        cartsList.stream()
                .forEach(cart -> {
                    //查询对应商品信息
                    Goods goods = goodsService.selectById(cart.getGoodsId());
                    cart.setGoods(goods);
                });
        return cartsList;
    }

    @Override
    public Cart selectById(Integer id) {
       return cartMapper.selectById(id);
    }

    @Override
    public boolean deleteBatch(List<Integer> ids,Integer userId) throws AllException {
        //判断该购物车是否存在
        for (Integer id : ids) {
            //判断该购物车是否存在
            Cart cart = cartMapper.selectById(id);
            if(cart == null){
                throw new AllException("该购物车记录不存在，无法删除");
            }
            //判断被删除的购物车是否属于当前用户
            if(!cart.getUserId().equals(userId)){
                throw new AllException("该购物车属于其他用户，无法删除");
            }
        }
        if(ids == null || ids.size() == 0){
            throw new AllException("无选中商品，无法删除");
        }
        return cartMapper.deleteBatch(ids) == ids.size();
    }

    @Override
    public List<Cart> selectByIds(Integer[] cartIds, Integer userId) throws AllException {
        List<Cart> cartList = cartMapper.selectByIds(cartIds);
        //查询出的购物车是否属于当前用户
        for (Cart cart : cartList) {
            if(!cart.getUserId().equals(userId)){
                throw new AllException("非法购物车");
            }
            //查询并设置购物车商品信息
            Goods goods = goodsService.selectById(cart.getGoodsId());
            cart.setGoods(goods);

        }
        return cartList;
    }
}
